package com.myk.game.heroscuffle.heroes;

import com.myk.game.heroscuffle.events.EventRunBack;
import com.myk.game.heroscuffle.events.HeroEvent_AfterDoAttack;
import com.myk.game.heroscuffle.game.Attacked;
import com.myk.game.heroscuffle.game.Element;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.utils.RdmPropUtil;

public class Hero_CangLang extends Hero {

    private final int ATK_ADD_UNIT = 10; //连续命中猎物时的伤害累加幅度
    private int hitCount = 0; //命中同一个猎物的次数

    public Hero_CangLang() {
        super("苍狼",
                "他更愿意去追捕猎物，连续命中相同的目标伤害会不断增加，击杀猎物后可恢复生命值。",
                Element.DARK,
                1200, 145, 52);
    }

    @Override
    public void onAutoSelectAction() {
        //更加优先的追捕猎物
        Hero prey = state.getAlivePrey(); //获取猎物
        Hero preAttacked = game.pool.getAnyoneAliveHero(this, prey); //其它目标
        if (preAttacked != null) {
            if (prey != null) {
                if (RdmPropUtil.bingo(11 / 13d)) {
                    doAttackActionStart(new Attacked(prey, "锁定目标持续追捕"));
                }
                else {
                    doAttackActionStart(new Attacked(preAttacked, "跟丢猎物只好攻击"));
                }
            }
            else {
                doAttackActionStart(new Attacked(preAttacked, "环顾四下决定猎捕"));
            }
        }
        else { //preAttacked为空还能攻击，prey必不为空
            doAttackActionStart(new Attacked(prey, "锁定目标持续追捕"));
        }
    }

    @Override
    public void onBeforeDoAttack(Hero attacked) {
        if (attacked == state.getAlivePrey()) {
            hitCount++;
            int atkAdd = hitCount * ATK_ADD_UNIT;
            atk += atkAdd;
            game.ui.printMsg(getObviousName() + "atk临时增加" + atkAdd +",当前" + atk + "点,");
            game.ui.sleepShortly();
            event.onAfterDoAttack.addRunBack(eventData -> {
                atk -= atkAdd; //攻击完成后记得还原攻击力
                eventData.removeRunBackFromListener();
            });
        }
        else {
            hitCount = 0;
        }
        attacked.beAttackActionStart(this); //重写记得回调此方法以继续流程
    }

    @Override
    public void onAfterDoAttack(Hero attacked, int damage, boolean isToDeath) {
        if (isToDeath && state.recentPrey == attacked) { //猎物死亡时可以啃食回血
            final int addHp = 100;
            if (hp + addHp < HP) {
                hp += addHp;
                game.ui.printMsg(getObviousName() + "嗜血成性啃食猎物hp恢复" + addHp + ",当前" + hp + "点。");
                game.ui.sleepShortly();
            }
            else {
                int lastHpAdd = getHpUpTo();
                hp += lastHpAdd;
                game.ui.printMsg(getObviousName() + "嗜血成性啃食猎物hp恢复" + lastHpAdd + ",当前" + hp + "点(已满)。");
                game.ui.sleepShortly();
            }
        }
    }
}
